package com.hanhanxiaochong.zhongxiangchuanmei.view.main.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.VipGradeItem;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.Utils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.view.MyApplication;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.act.HotProjectDetailActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.List;

/**
 * Created by RockPhoenix on 2018/6/19.
 */

public class HomeVipPageAdapter extends PagerAdapter {

    private Context context;
    private List<VipGradeItem> list;

    public HomeVipPageAdapter(Context context, List<VipGradeItem> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        VipGradeItem item = list.get(position);
        View view = ResourcesUtils.inflate(R.layout.item_home_vip);
        LinearLayout linearLayout = view.findViewById(R.id.ll_authority_container);
        List<VipGradeItem.GradeBean> gradeBeanList = item.getGrade();
        linearLayout.removeAllViews();
        View header = ResourcesUtils.inflate(R.layout.header_home_vip);
        ImageView ivBackground = header.findViewById(R.id.iv_vip);
        GlideImgManager.glideLoader(MyApplication.instance(), RetrofitSource.image_url + item.getPic(), R.mipmap.img_error, R.mipmap.img_error, ivBackground, 1);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ivBackground.getLayoutParams();
        int screenWidth = Utils.getScreenWidth(context);
        layoutParams.width = (int) (screenWidth * 0.8);
        layoutParams.height = layoutParams.width / 7 * 3;
        ivBackground.requestLayout();
        linearLayout.addView(header);
        if (gradeBeanList != null) {
            for (VipGradeItem.GradeBean bean : gradeBeanList) {
                if (TextUtils.isEmpty(bean.getTitle())) {
                    View goods = ResourcesUtils.inflate(R.layout.item_onlyvip);
                    ImageView ivPic = goods.findViewById(R.id.iv_good_pic);
                    TextView goodName = goods.findViewById(R.id.tv_good_name);
                    TextView normalPrice = goods.findViewById(R.id.tv_normal_price);
                    TextView projectFit = goods.findViewById(R.id.tv_project_fit);
                    TextView projectTime = goods.findViewById(R.id.tv_project_time);

                    GlideImgManager.glideLoader(context, RetrofitSource.image_url + bean.getPic().split(",")[0], R.mipmap.img_error, R.mipmap.img_error, ivPic);
                    goodName.setText(bean.getName());
                    ImageView selfSupport = goods.findViewById(R.id.iv_self_support);
                    selfSupport.setVisibility(bean.getSelf_support().equals("y") ? View.VISIBLE : View.GONE);
                    normalPrice.setText(String.format(ResourcesUtils.getString(R.string.normal_good_price), bean.getPrice()));
                    projectFit.setText(String.format(ResourcesUtils.getString(R.string.project_fit), bean.getSymptom_lists()));
                    projectTime.setText(String.format(ResourcesUtils.getString(R.string.project_time), bean.getWhen_long()));
                    goods.setOnClickListener(v -> context.startActivity(HotProjectDetailActivity.createIntent(context, bean.getId())));
                    linearLayout.addView(goods);
                } else {
                    View authority = ResourcesUtils.inflate(R.layout.rl_text_tag);
                    TextView textView = authority.findViewById(R.id.tv_tag);
                    textView.setText(bean.getTitle());
                    linearLayout.addView(authority);
                }
            }
        }
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}
